package cn.it.controller;

import cn.it.constant.MessageConstant;
import cn.it.entity.PageResult;
import cn.it.entity.QueryPageBean;
import cn.it.entity.Result;
import cn.it.pojo.Order;
import cn.it.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 预约列表管理
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    /**
     * 条件查询数据
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("queryPageBean = " + queryPageBean);
        try {
            PageResult result = orderService.findPage(queryPageBean);
            System.out.println("result = " + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 确定预约操作
     * @param id
     * @return
     */
    @RequestMapping("/ConfirmAppointment")
    public Result ConfirmAppointment(Integer id){
        System.out.println("id = " + id);
        try {
            orderService.ConfirmAppointment(id);
            System.out.println(MessageConstant.ORDERSETTING_SUCCESS);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(MessageConstant.ORDERSETTING_FAIL);
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 取消预约操作
     */
    @RequestMapping("/cancelReservation")
    public Result cancelReservation(Integer id){
        System.out.println("id = " + id);
        try {
            orderService.cancelReservation(id);
            System.out.println(MessageConstant.CANCELORDER_SUCCESS);
            return new Result(true, MessageConstant.CANCELORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(MessageConstant.CANCELORDER_FULL);
            return new Result(false,MessageConstant.CANCELORDER_FULL);
        }
    }

    /**
     * 添加预约
     */
    @RequestMapping("/add")
//    public Result add(Integer[] setmealIds,String order_Date,@RequestBody Member member){
    public Result add(@RequestBody Map map){
        map.put("setmealId",map.get("setmealId").toString().substring(1,2));

        Result result = null;
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            result = orderService.order(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(MessageConstant.ORDERSETTING_FAIL);
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }


    /**
     * 批量取消预约
     * @param orderIds
     * @return
     */
    @RequestMapping("/batchCancelReservation")
    public Result batchCancelReservation(Integer[] orderIds){
        System.out.println("orderIds = " + Arrays.deepToString(orderIds));
        try {
            orderService.batchCancelReservation(orderIds);
            System.out.println(MessageConstant.CANCELORDER_SUCCESS);
            return new Result(true, MessageConstant.CANCELORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(MessageConstant.CANCELORDER_FULL);
            return new Result(false,MessageConstant.CANCELORDER_FULL);
        }
    }


}
